<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { queryDataACWorthTrend, queryDataNetWorthTrend } from '@/api/front/fund/fundDataContent'

const animationDuration = 1000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '450px'
    },
    codeNumber: {
      type: String,
      default: ''
    },
    dataACAndNetWorth: {
      type: Object,
      default: undefined
    }
  },
  data() {
    return {
      chart: null,
      chartData: []
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    // 初始化 Echarts
    initChart() {
      // 初始化 chart
      this.chart = echarts.init(this.$el, 'macarons')

      // 获取数据
      queryDataNetWorthTrend(this.codeNumber).then(res => {
        this.dataAcWorthTrend = res
        // 获取数据
        this.chartData = this.resolveResult(res)
        // 告诉父组件数据信息
        this.$emit('getDataNetWorthTrendData', res)
        // chart 数据渲染
        this.setChartOption()
      })
    },

    // 转换数据
    resolveResult(res) {
      return res.map((item) => {
        const now = new Date(Number(item.x))
        return [
          [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'),
          Number(item.y)
        ]
      })
    },

    // 设置 Echarts option
    setChartOption() {
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          position: function(pt) {
            return [pt[0], '10%']
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          // bottom: '3%',
          containLabel: true
        },
        title: {
          left: 'center',
          text: '单位净值走势'
        },
        toolbox: {
          feature: {
            dataZoom: {
              yAxisIndex: 'none'
            },
            restore: {},
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false
        },
        yAxis: {
          type: 'value',
          boundaryGap: [0, '100%']
        },
        dataZoom: [
          {
            type: 'inside',
            start: 99,
            end: 100
          }, {
            start: 99,
            end: 100
          }
        ],
        series: [
          {
            name: '单位净值',
            type: 'line',
            smooth: true,
            symbol: 'none',
            areaStyle: {
              opacity: 0.5,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: 'rgb(46,198,232)'
              }, {
                offset: 1,
                color: 'rgb(179,228,245)'
              }])
            },
            data: this.chartData
          }
        ],
        animationDuration
      })
    }
  }
}
</script>
